package com.levy.schedule;

import java.text.SimpleDateFormat;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.junit.Test;

public class ScheduledExecutorServiceJDK {
	
	@Test
	public static void executeEightAtNightPerDay() {
		ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
		long oneDay = 1000;
		long initDelay = 1000 * 10;
		initDelay = initDelay > 0 ? initDelay : oneDay + initDelay;

		executor.scheduleAtFixedRate(new EchoServer(), initDelay, oneDay, TimeUnit.MILLISECONDS);
	}
	
	
	public static void main(String[] args){
		ScheduledExecutorServiceJDK.executeEightAtNightPerDay();
	}
}


class EchoServer implements Runnable {  
    public void run() {  
        try {  
            Thread.sleep(50);  
        } catch (InterruptedException e) {  
            e.printStackTrace();  
        }  
        System.out.println("This is a echo server. The current time is " +  
                System.currentTimeMillis() + ".");  
    }  
}  